package dev.werillib.camera

import android.content.Context
import android.os.Environment
import android.text.TextUtils
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File

object Compress {
    private fun getPath(): String {
        val path = Environment.getExternalStorageDirectory().toString() + "/weril/compress/image/"
        val file = File(path)
        return if (file.mkdirs()) {
            path
        } else path
    }

    fun compressImage(context: Context, file: File, onCompressListener: OnCompressListener) {
        Luban.with(context)
            .load(file)
            .ignoreBy(100)
            .setTargetDir(getPath())
            .setFocusAlpha(false)
            .filter { path -> !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif")) }
            .setCompressListener(onCompressListener).launch()
    }
}